package davidlauzon.activerecord.recordset;

import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


public class AndroidSqliteRecordSet extends RecordSet
{
    /*********************************************************************************************
     * CONSTANTS
     *********************************************************************************************/
    static private final DateFormat ISO_8601_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
    
    
    /*********************************************************************************************
     * VARIABLES
     *********************************************************************************************/
    private SQLiteDatabase _db;
    private Cursor  _cursor;
    
    
    
    /*********************************************************************************************
     * PUBLIC METHODS
     *********************************************************************************************/
    public AndroidSqliteRecordSet(Cursor cursor, SQLiteDatabase db) {
        _db      = db;
        _cursor  = cursor;
    }
    
    @Override
    public boolean next() throws SQLException
    {
    	if(_cursor.isClosed() || !_db.isOpen())
    		return false;
    	
        boolean hasNext = _cursor.moveToNext();
        
        if (!hasNext)       // Automatically close the result set if there is no more results
            close();
        
        return hasNext;
    }
    
    public boolean hasNext() throws SQLException
    {
    	if(_cursor.isClosed() || !_db.isOpen())
    		return false;
    	
    	return (_cursor.getCount() - _cursor.getPosition() > 1);
    }

    @Override
    public void close() { _cursor.close(); }

    @Override
    public boolean getBoolean(int index) throws SQLException { return _cursor.getInt(index) > 0; }

    @Override
    public float getFloat(int index) throws SQLException { return _cursor.getFloat(index); }

    @Override
    public double getDouble(int index) throws SQLException { return _cursor.getDouble(index); }

    @Override
    public int getInt(int index) throws SQLException { return _cursor.getInt(index); }

    @Override
    public long getLong(int index) throws SQLException { return _cursor.getLong(index); }

    @Override
    public String getString(int index) throws SQLException { return _cursor.getString(index); }

    public int getSize() { return _cursor.getCount(); }
    public int getPosition() { return _cursor.getPosition(); }
    
    @Override
    public Date getDate(int index) throws SQLException
    {
        // http://sberka.blogspot.com/2009/07/date-time-sqlite-and-android.html
        
        Date   date     = null;
        String datetime = _cursor.getString(index);
        
        try {
            date = ISO_8601_DATE_FORMAT.parse(datetime);
        } catch (ParseException e) {
            throw new SQLException("Parsing ISO-8601 datetime failed");
        }

        return date;
    }
    
    @Override
    public boolean lastColumnReadWasNull() throws SQLException {
        System.err.println("WARNING: " + getClass().getSimpleName() + 
                "::lastColumnReadWasNull() is unimplemented yet." );
        return false;
    }

}
